<template>
  <div>
    <el-dialog
        :visible.sync="dialogVisible"
        width="600px"
        :before-close="handleClose">
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="头像">
          <el-upload
              class="avatar-uploader"
              action="https://jsonplaceholder.typicode.com/posts/"
              :show-file-list="false"
              :http-request="beforeAvatarUpload">
            <img v-if="imageUrl" :src="imageUrl" class="avatars">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="账号">
          <el-input v-model="form.username"></el-input>
        </el-form-item>
        <el-form-item label="联系电话">
          <el-input v-model="form.phone"></el-input>
        </el-form-item>
        <el-form-item label="选择性别">
          <el-radio-group v-model="form.gender">
            <el-radio :label="0">男</el-radio>
            <el-radio :label="1">女</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="出生时间">
          <el-date-picker type="date" placeholder="选择日期" v-model="form.birthday" style="width: 100%;"></el-date-picker>
        </el-form-item>
        <el-form-item label="居住地址">
          <el-input v-model="form.remake"></el-input>
        </el-form-item>
        <el-form-item label="姓名">
          <el-input v-model="form.realName"></el-input>
        </el-form-item>
        <el-form-item label="身份证号">
          <el-input v-model="form.idCard"></el-input>
        </el-form-item>
        <el-form-item label="月薪">
          <el-input v-model="form.note"></el-input>
        </el-form-item>
        <el-form-item label="兴趣爱好">
          <el-input type="textarea" :rows="5" v-model="form.habits"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="danger" @click="onSubmit">立即提交</el-button>
          <el-button @click="handleClose">取消修改</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import {updateUser, uploadFile} from "@/api/apiFox";

export default {
  props:['isShow','userForm'],
  data(){
    return{
      dialogVisible:false,
      imageUrl: '',
      form: {
        birthday: "",
        note:"",//月薪
        gender: 0,
        habits: "",
        role:0,
        realName:"",
        icon: "",
        idCard: "",
        password: "",
        phone: "",
        username: "",
        remake:""
      }
    }
  },
  methods:{
    beforeAvatarUpload(file) {
      uploadFile(file).then(res=>{
        console.log(res)
        this.form.icon = res
        this.imageUrl = res
      }).catch(err=>{})
    },
    handleClose(){
      this.dialogVisible=false
      for(let key in this.form){
        this.form[key] = ""
      }
      this.$emit('closeForm',true)
    },
    onSubmit() {
      console.log('submit!');
      updateUser(this.form).then(res => {
        this.$message.success('修改成功')
        this.handleClose()
      }).catch(err => {})
    }
  },
  mounted() {
    if (this.isShow){
      this.dialogVisible = true
      this.form = this.userForm
      this.imageUrl = this.userForm.icon
    }else{
      this.dialogVisible = false
    }
  },

  watch:{
    isShow(newValue,oldValue){
      if (this.isShow){
        this.dialogVisible = true
        this.form = this.userForm
        this.imageUrl = this.userForm.icon
      }else{
        this.dialogVisible = false
      }
    }
  }
}
</script>

<style scoped lang="scss">
::v-deep(.el-dialog){
  border-radius: 30px;
}
.avatar-uploader{
  width: 150px;
  height: 150px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  border: 1px dashed #d9d9d9;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 150px;
  height: 150px;
  line-height: 150px;
  text-align: center;
}
.avatars {
  width: 150px;
  height: 150px;
  border-radius: 50%;
  display: block;
}
</style>
